from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QPushButton, QFileDialog, \
    QProgressBar
from PyQt5.QtCore import Qt
from TransferTools.appfunction import send_file
from pathlib import Path
import threading

class SendWindow(QWidget):
    def __init__(self, controller):
        super(SendWindow, self).__init__()

        self.controller = controller
        self.container = QVBoxLayout()
        self.back_button = QPushButton("返回")
        self.select_file_button = QPushButton("选择文件")
        self.process_view = QProgressBar()
        self.process_view.setRange(0, 100)
        self.process_view.setValue(0)
        self.status = [0]


        self.equip_view()
        self.function_init()



    def equip_view(self):
        self.setLayout(self.container)

        # 层级关系
        self.container.addStretch(1)
        self.container.addWidget(self.select_file_button)
        self.container.addWidget(self.back_button)
        self.container.addWidget(self.process_view)
        self.container.addStretch(1)

        # 显示风格
        self.process_view.setAlignment(Qt.AlignCenter)
        self.process_view.hide()

    def function_init(self):
        self.back_button.clicked.connect(self.back_button_func)
        self.select_file_button.clicked.connect(self.select_file_button_func)

    def back_button_func(self):
        self.controller.change_view(0)

    def processing_view(self):
        value = self.status[0]
        self.process_view.setValue(value)
        return value

    def select_file_button_func(self):
        file_path, _ = QFileDialog.getOpenFileName(
            self,
            "选择文件",
            "",
            "所有文件 (*);;文本文件 (*.txt);;Python文件 (*.py)"
        )
        if file_path:
            file_thread = threading.Thread(target=send_file, args=(Path(file_path), self))
            file_thread.start()
            self.process_view.show()
            self.process_view.setValue(0)

